package com.maigao.util;

import java.time.LocalDate;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2021-11-09 17:03
 **/
public class HolidayUtils {


    /**
     * 判断是否是节假日
     *
     * @return
     */
    public static boolean isHoliday(LocalDate localDate) {
        try {
//            Thread.sleep(500);
//            String date = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            // 接口限制最大1秒两次, 否则封1天
            String url = String.format("http://timor.tech/api/holiday/info/%s", localDate.toString());
            String result = HttpUtils.doGet(url, null, null);
            HolidayDto holidayDto = JsonUtil.toBean(result, HolidayDto.class);

            if (holidayDto.getCode()==0) {
                if (holidayDto.getType().getType()==0) {
                    return false;
                }
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isWeekend(localDate);
    }

    public static boolean isWeekend(LocalDate localDate) {
        int day = localDate.getDayOfWeek().getValue();
        if (day == 6 || day == 7) {
            return true;
        }
        return false;
    }

    public static boolean isHoliday() {
        return isHoliday(LocalDate.now());
    }

    public static LocalDate getNextHolidy() {
        LocalDate now = LocalDate.now();
        while (true) {
            LocalDate nextDate = now.plusDays(1);
            if (!isHoliday(nextDate)) {
                return nextDate;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(getNextHolidy());
    }
}
